package com.kx.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.pojo.User;
import com.kx.smartdevice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Author Sun
 * @Date 2021/5/26 15:12
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public  R findAllUsersByPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,String username,@RequestParam(defaultValue = "-100") int status){
        PageInfo<User> pageInfo = userService.getAllUsersByPage(page, limit,username,status);
        return R.setOK(pageInfo);
    }

    @PutMapping("/user")
    public R addUser(@RequestBody User user){
        userService.addUser(user);
        return R.setOK(null);
    }
    @PostMapping("/user")
    public R updateUser(@RequestBody User user){
        int result = userService.updateUser(user);
        return R.setOK(result);
    }

    @DeleteMapping("/users")
    public R deleteUserById(String[] ids){
       int result = userService.deleteUserByIds(ids);
       return R.setOK(result);
    }
}
